public class Work2 {
    public static void main1(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[20];
        for (int i = 0; i < threads.length; i++) {
            final int n = i;
            threads[i] = new Thread(() -> {
                System.out.println(n);
            });
        }
        for (Thread thread:threads) {
            thread.start();
        }
        for (Thread thread:threads) {
            thread.join();
        }
        System.out.println("ok");
    }

    public static int count = 0;
    public final static Object locker = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 10000; i++) {
                    count++;
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 10000; i++) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t1.join();
        System.out.println("count = " + count);
    }
}
